/*
 * BanBu_ImageViewCell
 * @description 本文件提供聊天界面对话界面显示图片消息的cell
 * @package
 * @author 		***
 * @copyright
 * @version 		1.0
 * @description 本文件提供聊天界面对话界面显示图片消息的cell
 */


#import "BanBu_ImageViewCell.h"
#import "AppDataManager.h"
#import "BanBuAPIs.h"
#import "AppCommunicationManager.h"

@implementation BanBu_ImageViewCell

-(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self)
    {
        //图片
        _mediaView = [[BanBu_MediaView alloc] initWithFrame:CGRectZero];
        _mediaView.showButton.imageView.layer.cornerRadius = 12;
        
        [self addSubview:_mediaView];
        
        _activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
        [_activityIndicator setFrame:CGRectMake(0, 17, 30, 30)];
        _activityIndicator.center = self.bkView.center;
        [_activityIndicator setHidesWhenStopped:YES];
        [_activityIndicator setColor:[UIColor blackColor]];
        
        [_activityIndicator  startAnimating];
        [self addSubview:_activityIndicator];
        
        //阅后即焚消息倒计时背景
        _backgroundTime = [[UIImageView alloc]initWithFrame:CGRectZero];
        _backgroundTime.backgroundColor = [UIColor clearColor];
        _backgroundTime.image = [UIImage imageNamed:@"sexangle_black.png"];
        _backgroundTime.hidden = YES;
        [self addSubview:_backgroundTime];
        
        _snapTimeLabel = [[UILabel alloc]initWithFrame:CGRectMake(self.bkView.frame.origin.x+5, self.bkView.frame.origin.y-2, 20, 20)] ;
        _snapTimeLabel.font = [UIFont systemFontOfSize:12];
        _snapTimeLabel.textColor = [UIColor whiteColor];
        _snapTimeLabel.backgroundColor = [UIColor clearColor];
        _snapTimeLabel.textAlignment = UITextAlignmentCenter;
        _snapTimeLabel.hidden = YES;
        [self addSubview:_snapTimeLabel];
    }
    return self;
}

-(void)dealloc
{
    [_mediaView release],_mediaView = nil;
    [_activityIndicator release],_activityIndicator = nil;
    [_backgroundTime release],_backgroundTime = nil;
    [_snapTimeLabel release],_snapTimeLabel = nil;
    [super dealloc];
}

-(void)layoutSubviews
{
    [super layoutSubviews];
    
    float height = self.contentView.frame.size.height;
    
    _mediaView.frame = ImageViewRext00;
    
    //如果是破冰语的gif图，则_bkView.alpha = 0；
    if ([[_mediaView.mediaPath pathExtension] isEqualToString: @"gif"])
    {
        self.bkView.alpha = 0;
        _activityIndicator.center = _mediaView.center;
    }
    else
    {
        self.bkView.alpha = 1;
        _activityIndicator.center = self.bkView.center;
    }
    
    UIImage * image = nil;//内容是图片时，content为图片的路径
    
    
    if (!image && [self.from isEqualToString:@"store"])//这是表情商店的表情
    {
        NSArray * arr = [self.content pathComponents];
        NSString * imageName = [self.content lastPathComponent];
        NSString * folderName = [arr objectAtIndex:arr.count-2];
        
        NSString * imagePath = [AppComManager pathForEmo:imageName withFolder:folderName];
        
        image = [UIImage imageWithContentsOfFile:imagePath];
    }
    else if(!image && [self.from isEqualToString:@"collect"])
    {
        NSString * imagePath = [AppComManager pathForCollect:self.content];
        
        image = [UIImage imageWithContentsOfFile:imagePath];
    }
    else if (self.content.length)
    {
        image = [MyAppDataManager msgImageForImageUrlStr:self.content];
    }
    
    
    if (image)
    {
        double bl = 1;
        bl = image.size.width/image.size.height;
        
        if (bl<0.9)//图片比较高，固定宽度：77
        {
            _mediaView.frame = CGRectMake(_mediaView.frame.origin.x, _mediaView.frame.origin.y, 77, 77/bl);
            self.bkView.frame = CGRectMake(self.bkView.frame.origin.x, self.bkView.frame.origin.y, 95+10, 77/bl+22);
        }
        else if (bl >= 0.9 && bl <= 1.1 )//图片是正方形的，固定宽度：60
        {
            self.bkView.frame = BKViewRect00(self.bkViewWidth, height-2*CellMarge);
            
            CGRect frame = _mediaView.frame;
            frame.size.width -=2;
            frame.size.height -=2;
            _mediaView.frame = frame;
        }
        else//图片比较长，固定长度，140，这是发送闪光字
        {
            if (image.size.width>MaxGIFWith)
            {
                if (self.bkView.alpha)
                {
                    self.bkView.frame = BKViewRect02(MaxGIFWith, MaxGIFWith/bl);
                }
                else
                {
                    self.bkView.frame = BKViewRect01(MaxGIFWith + 10, MaxGIFWith/bl);
                }
            }
            else
            {
                if (self.bkView.alpha)
                {
                    self.bkView.frame = BKViewRect02(self.bkViewWidth, MaxGIFWith/bl);
                }
                else
                {
                    self.bkView.frame = BKViewRect01(self.bkViewWidth, MaxGIFWith/bl);
                }
            }
            
            self.mediaView.frame = ImageViewRext00;
            
            if (image.size.width>MaxGIFWith)
            {
                _mediaView.frame = CGRectMake(_mediaView.frame.origin.x, _mediaView.frame.origin.y, 147, _mediaView.frame.size.width/bl);
                self.bkView.frame = CGRectMake(self.bkView.frame.origin.x, self.bkView.frame.origin.y, 175, 147/bl+22);
            }
            else
            {
                _mediaView.frame = CGRectMake(_mediaView.frame.origin.x, _mediaView.frame.origin.y, image.size.width-8, image.size.width/bl);
                self.bkView.frame = CGRectMake(self.bkView.frame.origin.x, self.bkView.frame.origin.y, image.size.width+20, image.size.width/bl+21);
            }
        }
        
        if (self.bkView.alpha == 0)
        {
            if (self.atLeft)
            {
                _mediaView.frame = CGRectMake(_mediaView.frame.origin.x-10, _mediaView.frame.origin.y,_mediaView.frame.size.width,_mediaView.frame.size.height);
            }
            else
            {
                _mediaView.frame = CGRectMake(_mediaView.frame.origin.x+40, _mediaView.frame.origin.y,_mediaView.frame.size.width,_mediaView.frame.size.height);
            }
        }
        else
        {
            CGRect frame = _mediaView.frame;
            frame.origin.y += 5;
            if (!self.atLeft)
            {
                frame.origin.x += 4;
            }
            _mediaView.frame = frame;
        }
    }
    
    //如果显示from
    if (self.showFrom)
    {
        [self setShowFromFrame:ChatMsgMediaView];
        if (_mediaView.snapMsg)
        {
            CGRect frame = self.bkView.frame;
            if (self.atLeft)
            {
                _snapTimeLabel.frame = CGRectMake(frame.origin.x+frame.size.width+5, frame.origin.y-2, 20, 20);
                _backgroundTime.frame = CGRectMake(frame.origin.x+frame.size.width+5, frame.origin.y-2, 20, 20);
            }
            else
            {
                _snapTimeLabel.frame = CGRectMake(frame.origin.x+5, frame.origin.y-2, 20, 20);
            }
        }
    }
    
    CGFloat f=[self.statusLabel.text sizeWithFont:[UIFont systemFontOfSize:11]].width;
    CGRect frame = StatusViewRect;
    frame.origin.x = frame.origin.x - f + 27;
    frame.size.width = f + 3;
    
    if (self.bkView.alpha == 0 && !self.atLeft && ([self.from isEqualToString:@"pb"]|| [self.from isEqualToString:@"mg"]))
    {
        frame.origin.x += 10;
        self.statusView.frame = frame;
    }
    else if ([self.from isEqualToString:@"store"]&& !self.atLeft)
    {
        frame.origin.x += 10;
        self.statusView.frame = frame;
    }
    else
    {
        self.statusView.frame = frame;
    }
    self.avatar.frame = AvatarRect01;
    
    if (!self.atLeft)
    {
        _mediaView.frame = CGRectMake(self.bkView.frame.origin.x + 15, self.bkView.frame.origin.y + 10, self.bkView.frame.size.width - 28, self.bkView.frame.size.height - 20);
    }
    
    
}

//设置cell的各种信息
-(void)setChatCellInfo:(NSDictionary *)amsg
{
    self.bkViewWidth = 90.0;
    _mediaView.appChatController = (BanBu_ChatViewController *)self.delegate;
    
    //设置父类的属性
    [super setChatCellInfo:amsg];
    
    MediaStatus status = [VALUE(KeyMediaStatus, amsg) integerValue];
    NSString *content = VALUE(KeyContent, amsg);
    
    _effectiveTime = [[amsg valueForKey:KeyEffectiveTime] integerValue];
    _mediaView.snapMsg = [VALUE(KeyShowFrom, amsg) isEqualToString:@"snap"];
    
//    [_mediaView setMedia:content];
    [_mediaView setMedia:content withFrom:self.from];
    [_mediaView setStatus:status];
    [self setMsgMediastatus:status];
    
    if(status == MediaStatusDownload)
    {
        [AppComManager getBanBuMedia:VALUE(KeyContent, amsg) forMsgID:VALUE(KeyChatid, amsg) fromUid:MyAppDataManager.chatuid delegate:MyAppDataManager];
    }
    else
    {
        [self getImage:amsg];
    }
}

-(void)getImage:(NSDictionary *)amsg
{
    UIImage * image = nil;//内容是图片时，content为图片的路径
    NSString * imagePath = nil;
    
    if (!image && [self.from isEqualToString:@"store"])//这是表情商店的表情
    {
        NSArray * arr = [self.content pathComponents];
        NSString * imageName = [self.content lastPathComponent];
        NSString * folderName = [arr objectAtIndex:arr.count-2];
        
        imagePath = [AppComManager pathForEmo:imageName withFolder:folderName];
        image = [UIImage imageWithContentsOfFile:imagePath];
        
        if (![FileManager fileExistsAtPath:imagePath])
        {
            //网络路径
            [AppComManager getEmoImage:self.content withImageName:imageName withFolder:folderName delegate:MyAppDataManager withImageView:nil];
        }
    }
    else if (!image && [self.from isEqualToString:@"collect"])//收藏的表情
    {
        NSString * imagePath = [AppComManager pathForCollect:self.content];
        image = [UIImage imageWithContentsOfFile:imagePath];
        
        if (![FileManager fileExistsAtPath:imagePath])
        {
            //网络路径
            
            [AppComManager downloadAmsgFileWithUrl:self.content withUserId:nil withFolder:CollectionFolder withDelegate:MyAppDataManager];
        }
        
    }
    else if (!image && [self.from isEqualToString:@"collectimage"])//收藏的图片
    {
        NSString * imagePath = [AppComManager pathForCollectImage:self.content];
        image = [UIImage imageWithContentsOfFile:imagePath];
        
        if (![FileManager fileExistsAtPath:imagePath])
        {
            //网络路径
            [AppComManager downloadAmsgFileWithUrl:self.content withUserId:nil withFolder:ManuscriptFolder withDelegate:MyAppDataManager];
        }

    }
    else if(self.content.length)
    {
        image = [MyAppDataManager msgImageForImageUrlStr:self.content];
        if (!image)
        {
            [AppComManager getBanBuMedia:VALUE(KeyContent, amsg) forMsgID:VALUE(KeyChatid, amsg) fromUid:MyAppDataManager.chatuid delegate:MyAppDataManager];
        }
    }
}

-(void)setMsgMediastatus:(MediaStatus)status
{
    if(status != MediaStatusNormal && status != MediaStatusReaded)//不是：已播过或正常时，loading转圈
    {
        [_activityIndicator  startAnimating];
    }
    else
    {
        [_activityIndicator  stopAnimating];
    }
}
@end
